package parser

import (
	"crowler/engine"
	"regexp"
	"strconv"
	"crowler/model"
)
var GenderRe       = regexp.MustCompile(`<td><span class="label">性别：</span><span field="">([^<]+)</span></td>`)
var AgeRe          = regexp.MustCompile(`<td><span class="label">年龄：</span>([\d]+)岁</td>`)
var HeightRe       = regexp.MustCompile(`<td><span class="label">身高：</span>([\d]+)CM</td>`)
var WeightRe       = regexp.MustCompile(`<td><span class="label">体重：</span><span field="">([\d]+)KG</span></td>`)
var IncomeRe       = regexp.MustCompile(`<td><span class="label">月收入：</span>([^<]+)</td>`)
var MarriageRe     = regexp.MustCompile(`<td><span class="label">婚况：</span>([^<]+)</td>`)
var EducationRe    = regexp.MustCompile(`<td><span class="label">学历：</span>([^<]+)</td>`)
var OccupationRe   = regexp.MustCompile(`<td><span class="label">职业： </span>([^<]+)</td>`)
var HokouRe        = regexp.MustCompile(`<td><span class="label">籍贯：</span>([^<]+)</td>`)
var XinzuoRe       = regexp.MustCompile(`<td><span class="label">星座：</span><span field="">([^<]+)</span></td>`)
var HouseRe        = regexp.MustCompile(`<td><span class="label">住房条件：</span><span field="">([^<]+)</span></td>`)
var CarRe          = regexp.MustCompile(`<td><span class="label">是否购车：</span><span field="">([^<]+)</span></td>`)


//函数
func ParseProfile(contents []byte , name string) engine.ParseResult {



	profile := model.Profile{}

	profile.Name = name

	profile.Gender = extractString(contents,GenderRe)

	age,err := strconv.Atoi(extractString(contents,AgeRe))  //整数
	if err == nil{
		profile.Age = age
	}

	height,err := strconv.Atoi(extractString(contents,HeightRe))  //整数
	if err == nil{
		profile.Height = height
	}

	weight,err := strconv.Atoi(extractString(contents,WeightRe))  //整数
	if err == nil{
		profile.Weight = weight
	}

	profile.Income = extractString(contents,IncomeRe)

	profile.Marriage = extractString(contents,MarriageRe)

	profile.Education = extractString(contents,EducationRe)

	profile.Occupation = extractString(contents,OccupationRe)

	profile.Hokou = extractString(contents,HokouRe)

	profile.Xinzuo = extractString(contents,XinzuoRe)

	profile.House = extractString(contents,HouseRe)

	profile.Car = extractString(contents,CarRe)

	result := engine.ParseResult{
		Items:[]interface{}{profile},
	}

	return result
}



func extractString(contents []byte,re *regexp.Regexp)  string{

    match := re.FindSubmatch(contents) //原字符串和 匹配之后的结果

    if len(match) >= 2 {
    	return string(match[1])
	}else{
		return  ""
	}

}	